package com.easemob.helpdesk.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.easemob.helpdesk.adapter.ServiceFeeAdapter;
import com.easemob.helpdesk.bean.ServiceFeeBean;
import com.housekeeperapp.R;
import com.net.ApiService;
import com.net.BaseResponse;
import com.net.HKHttpUtils;
import com.net.HKResponseResultCallBack;

import java.util.ArrayList;
import java.util.List;


import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by xiaolong.wei on 2018/12/17.
 */

public class ServiceFeeInstruction extends BaseActivityV2 {
    private static final String TAG = "ServiceFeeInstruction";
    ListView mListView;
    TextView mTime;
    ServiceFeeAdapter mAdapter;

    List<ServiceFeeBean> mDatas;
    private String serviceFeeName;

    @Override
    protected void initFindView() {
        super.initFindView();
        mListView=findViewById(R.id.list);
        mTime=findViewById(R.id.time);
        findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.service_fee_instruction);
        initFindView();
        String selectedDate = getIntent().getStringExtra("selectedDate");
        String phone = getIntent().getStringExtra("phone");
        long groupId = getIntent().getIntExtra("groupId",-1);
        serviceFeeName = getIntent().getStringExtra("serviceFeeName");
        Log.d(TAG, "onCreate: selectedDate="+selectedDate);
        Log.d(TAG, "onCreate: phone="+phone);
        mDatas = new ArrayList<>();
        mAdapter = new ServiceFeeAdapter(mDatas,this);
        mListView.setAdapter(mAdapter);
        if (!TextUtils.isEmpty(selectedDate) && !TextUtils.isEmpty(phone) && groupId != -1){
            getData(phone,selectedDate,groupId);
        }else {
            Log.d(TAG, "onCreate: phone="+phone+"----selectedDate="+selectedDate);
        }
    }

/*    @OnClick(R.id.back)
    public void onClick(View view){
        switch (view.getId()){
            case R.id.back:
                finish();
                break;
            default:break;
        }
    }*/

    private void getData(String phone, String date,long groupId){
        ApiService apiService = HKHttpUtils.getApiService();
        apiService.getServiceFeeList(MainActivity.ticket,groupId,phone,date).enqueue(new HKResponseResultCallBack<ArrayList<ServiceFeeBean>>() {
            @Override
            public void onSuccess(ArrayList<ServiceFeeBean> serviceFeeBeans) {
                if (serviceFeeBeans == null || serviceFeeBeans.size() == 0){
                    Log.d(TAG, "onSuccess: 暂无列表");
                    return;
                }else {

                    if (serviceFeeBeans.get(0).getTimeRequired() == 1){
                        mTime.setVisibility(View.VISIBLE);
                        if (serviceFeeBeans.get(0) != null && !TextUtils.isEmpty(serviceFeeBeans.get(0).getStartTime()) && ! TextUtils.isEmpty(serviceFeeBeans.get(0).getEndTime())) {
                            String startTime = serviceFeeBeans.get(0).getStartTime();
                            String endTime = serviceFeeBeans.get(0).getEndTime();
                            mTime.setText((TextUtils.isEmpty(serviceFeeName)?"服务费 ":serviceFeeName+" ")+ startTime.substring(0,5)+"-"+endTime.substring(0,5));
                        }
                    }else {
                        mTime.setVisibility(View.GONE);
                    }
                    mDatas.clear();
                    mDatas.addAll(serviceFeeBeans);
                    mAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onGetDataFailed(int cd, String msg) {
                Log.d(TAG, "onGetDataFailed: "+msg);
            }

            @Override
            public void onFailed(Call call, Throwable t) {
                Log.d(TAG, "onFailed: "+t.getMessage());
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
